﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace TcpIpServerTest
{
    public class Utils
    {        
        public static void printIPs()
        {
            string name = Dns.GetHostName();
            IPAddress[] addrs = getIPs(name);
            if (addrs != null)
            {
                foreach (IPAddress addr in addrs)
                {
                    Console.WriteLine("{0}/{1}", name, addr);
                }
                printIPs();
            }
            else
            {
                Console.WriteLine("IP Address Fetch Error");
            }


            IPAddress test1 = IPAddress.Parse("192.168.1.1");
            IPAddress test2 = IPAddress.Loopback;
            IPAddress test3 = IPAddress.Broadcast;
            IPAddress test4 = IPAddress.Any;
            IPAddress test5 = IPAddress.None;
            IPHostEntry ihe =
                    Dns.GetHostByName(Dns.GetHostName());
            IPAddress myself = ihe.AddressList[0];
            if (IPAddress.IsLoopback(test2))
                Console.WriteLine("The Loopback address is: {0}",
                     test2.ToString());
            else
                Console.WriteLine("Error obtaining the loopback address");
            Console.WriteLine("The Local IP address is: {0}\n",
            myself.ToString());
            if (myself == test2)
                Console.WriteLine("The loopback address is the same as local address.\n");
            else
                Console.WriteLine("The loopback address is not the local address.\n");
            Console.WriteLine("The test address is: {0}",
                    test1.ToString());
            Console.WriteLine("Broadcast address: {0}",
                    test3.ToString());
            Console.WriteLine("The ANY address is: {0}",
                    test4.ToString());
            Console.WriteLine("The NONE address is: {0}",
                    test5.ToString());

        }
        public static IPAddress[] getIPs(String name)
        {
            try
            {
                IPAddress[] addrs = Dns.Resolve(name).AddressList;
                return addrs;
            }
            catch
            {
                return null;
            }
        }
        public static String getMyIp()
        {
            IPHostEntry ihe = Dns.GetHostByName(Dns.GetHostName());
            IPAddress myself = ihe.AddressList[0];
            return myself.ToString();
        }
        public static char[] byteArrayToCharArray(byte[] array)
        {
            char[] ch = new char[array.Length];
            for (int i = 0; i < array.Length; i++)
            {
                ch[i] = (char)array[i];
            }
            return ch;
        }
        public static byte[] stringToByteArray(String ack)
        {
            byte[] temp = new byte[ack.Length];
            for (int i = 0; i < ack.Length; i++)
            {
                temp[i] = (byte)ack[i];
            }
            return temp;
        }
    }
}
